package cn.cinz.filter;

import cn.cinz.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/***
 * 登陆拦截器配置
 * 避免未登录进入应用界面
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        String url = request.getRequestURI();
        System.out.println("拦截器--->(目前请求路径):"+url);
        //放行数据请求端口
        if (url.indexOf("/user/login") >= 0 || url.indexOf("/user/getSessionName") >= 0
                || url.indexOf("/user/userNameTest") >= 0 || url.indexOf("/user/userPhoneTest") >= 0
                || url.indexOf("/user/getCode") >= 0 || url.indexOf("/user/codeStatus") >= 0
                || url.indexOf("/user/deleteBlog") >= 0 || url.indexOf("/user/removeSession") >= 0
                || url.indexOf("/user/register") >= 0 || url.indexOf("/user/queryBlogList") >= 0) {
            return true;
        }

        if (url.indexOf("/user/updateBlog") >= 0){
            return true;
        }


        //验证用户是否登陆 成功
        Object o = request.getSession().getAttribute("userName");
        if (o != null) {
            return true;
        }


        //未登录用户 返回到登陆界面
        response.sendRedirect("../login.html");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
